<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px gw-flex-between"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <div>
        <el-form-item label="问题" prop="question">
          <el-input
            v-model="queryParams.question"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="prompt模板" prop="promptId"  label-width="100px">
          <el-input
            v-model="queryParams.promptId"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
      </div>
      <el-form-item>
        <div class="btn-group">
          <el-button @click="resetQuery" plain type="primary">重置</el-button>
          <el-button @click="handleQuery" type="primary">查询</el-button>
          <el-button
            type="primary"
            @click="goBack"
          >
            返回
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table :data="list" :stripe="true" :show-overflow-tooltip="true" :header-cell-style="{ 'background': '#CCEBE7', 'color': '#585858' }">
      <el-table-column label="序号" type="index" width="60px" align="center" />
      <!-- <el-table-column label="promptID" align="center" prop="id" /> -->
      <el-table-column label="问题" align="center" prop="question" />
      <el-table-column label="答案" align="center" prop="answer" />
      <el-table-column label="prompt模板" align="center" prop="promptName" />
      <el-table-column label="模板描述" align="center" prop="promptRemark" />
      <el-table-column label="操作" align="center" width="180px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('view', scope.row.id)"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DetailForm ref="formRef" @success="getList" />
  <DelConfirmDialog ref="delConfirmRef" @confirm="deleteConfirm" />
</template>

<script setup lang="ts">
import { DatasetItemApi, DatasetItemVO } from '@/api/multiModel/datasetitem'
import DetailForm from './DetailForm.vue'
import { useRoute, useRouter } from 'vue-router'

defineOptions({ name: 'DatasetDetail' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const route = useRoute()
const router = useRouter()
const list = ref<DatasetItemVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
interface queryParamsVO {
  pageNo: number,
  pageSize: number,
  datasetId?: number | undefined,
  question?: string | undefined,
  promptId?: string | undefined,
}
const queryParams = reactive<queryParamsVO>({
  pageNo: 1,
  pageSize: 10,
  datasetId: undefined,
  question: '',
  promptId: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  try {
    const data = await DatasetItemApi.getDatasetItemPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const delConfirmRef = ref()
const handleDelete = async (id: number) => {
  delConfirmRef.value.open(id, ['是否确定删除选中评测数据集?'])
}
/** 删除按钮操作 */
const deleteConfirm = async (id: number) => {
  try {
    await DatasetItemApi.deleteDatasetItem(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const goBack = () => {
  router.go(-1); // 返回上一页
};

/** 初始化 **/
onMounted(() => {
  queryParams.datasetId = Number(route.query.id)
  getList()
})
</script>
<style scoped lang="scss">
</style>